<template>
  <div class="tpl-container">
    <div class="mb20 condition-container">
      <!-- <div class="condition">
        <span class="color_494949 ft12 pr5">{{ translate('图片名称') }}：</span>
        <ElInput
          v-model="CSImageName"
          class="w164 mr14"
          :placeholder="translate('请输入图片名称')"
        />
      </div> -->
      <div class="condition">
        <!-- <ElButton class="settingBtn" type="primary" @click="search">
          {{ translate('查询') }}
        </ElButton> -->
        <ElButton
          v-if="MenuAuth('add')"
          class="settingBtn"
          plain
          type="primary"
          @click="addFn"
        >
          {{ translate('新增') }}
        </ElButton>
      </div>
    </div>
    <div class="table">
      <ElTable
        ref="multipleTableRef"
        v-loading="loading"
        :border="true"
        :data="tableData"
      >
        <ElTableColumn
          :label="translate('播放主键')"
          prop="TPID"
          width="auto"
        />
        <ElTableColumn
          :label="translate('开始时间')"
          prop="startdate"
          width="auto"
        />
        <ElTableColumn
          :label="translate('开始时间戳')"
          prop="startdate_timestamp"
          width="auto"
        />
        <ElTableColumn
          :label="translate('时间间隔')"
          prop="time_interval"
          width="auto"
        />
        <ElTableColumn
          :label="translate('播放时长')"
          prop="duration"
          width="auto"
        />
        <ElTableColumn :label="translate('播放类型')">
          <template #default="{ row }">
            <div>
              {{ row.type == 1 ? translate('图片') : translate('视频') }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn :label="translate('状态')">
          <template #default="{ row }">
            <div>
              {{
                row.status == 0
                  ? translate('待生效')
                  : row.status == 1
                  ? translate('立即生效')
                  : translate('已生效')
              }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn :label="translate('操作')" width="200">
          <template #default="{ row }">
            <ElButton
              v-if="MenuAuth('edit')"
              size="small"
              plain
              type="primary"
              @click="editFn(row)"
            >
              {{ translate('修改') }}
            </ElButton>
            <!-- <ElButton
              v-if="MenuAuth('delete')"
              link
              type="danger"
              @click="deleteFn(row)"
            >
              {{ translate('删除') }}
            </ElButton> -->
          </template>
        </ElTableColumn>
      </ElTable>
      <div v-if="total > 0" class="pagination">
        <ElPagination
          background
          :current-page="searchInfo.pageNo"
          :layout="layout"
          :page-size="searchInfo.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
  <Dialog
    :dialog-data="editData"
    :title="dialogType === 'edit' ? translate('编辑') : translate('新增')"
    :type="dialogType"
    :visible="dialogVisible"
    @close="handleClose"
    @confirm="handleConfirm"
  />
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import Dialog from './components/detail.vue'
  import { MenuAuth } from '~/src/utils/permission'
  import { CSTempPlayList, delcsimg, settempplay } from '@/api/mupro'
  import { translate } from '@/i18n'
  const CSImageName = ref('')
  const searchInfo = reactive({
    pageNo: 1,
    pageSize: 20,
    tableName: '',
    sort: '',
    order: '',
    wheres: `[{"name":"CSImageName","value":'',"displayType":"like"}]`,
    export: false,
    value: '',
  })
  const loading = ref(false)
  const dialogVisible = ref(false)
  const dialogType: any = ref('add')
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const tableData = ref([])
  const total = ref(0)
  // 获取表格数据
  const gettableData = async () => {
    loading.value = true
    const { data } = await CSTempPlayList(searchInfo)
    loading.value = false
    // console.log(data)
    tableData.value = data?.rows || []
    total.value = data?.total || 0
  }
  gettableData()
  //分页条数切换
  const handleSizeChange = (val: any) => {
    searchInfo.pageSize = val
    gettableData()
  }
  //分页切换
  const handleCurrentChange = (val: any) => {
    searchInfo.pageNo = val
    gettableData()
  }

  const search = () => {
    searchInfo.pageNo = 1
    if (CSImageName.value.trim() !== '') {
      const arr = []
      CSImageName.value.trim() !== '' &&
        arr.push({
          name: 'CSImageName',
          value: CSImageName.value,
          displayType: 'like',
        })
      searchInfo.wheres = JSON.stringify(arr)
    } else {
      searchInfo.wheres = `[{"name":"CSImageName","value": '',"displayType":"like"}]`
    }
    gettableData()
  }
  const editData: any = ref({})
  // 新增
  const addFn = async () => {
    dialogType.value = 'add'
    editData.value = {}
    dialogVisible.value = true
  }
  // 编辑
  const editFn = (row: any) => {
    editData.value = row
    dialogType.value = 'edit'
    dialogVisible.value = true
  }
  // 删除
  const $baseConfirm = inject('$baseConfirm') as any
  const deleteFn = async (row: any) => {
    $baseConfirm(translate('你确定要删除当前项吗'), null, async () => {
      // await delcsimg({
      //   id: row.ID,
      // })
      gettableData()
    })
  }
  const handleClose = () => {
    dialogVisible.value = false
  }
  // 弹窗确定回调
  const handleConfirm = async (data: any) => {
    data.time_interval = Number(data.time_interval)
    data.duration = Number(data.duration)
    console.log(data)
    await settempplay(data)
    dialogVisible.value = false
    gettableData()
  }
</script>

<style lang="scss" scoped>
  .tpl-container {
    .color_494949 {
      color: #494949;
    }

    .noData {
      text-align: center;
      font-size: 16px;
      color: gray;
    }
    .w164 {
      width: 164px;
    }
    .mb20 {
      margin-bottom: 20px;
    }

    .img-container {
      display: flex;
      // justify-content: center;

      .img {
        width: 100px;
      }
    }

    .condition-container {
      display: flex;
      flex-wrap: wrap;
      align-items: center;

      .condition {
        margin-right: 14px;
        margin-bottom: 10px;
      }
    }
  }
  .el-table {
    & ::v-deep th.el-table__cell,
    ::v-deep td.el-table__cell {
      // 设置position 使得 子元素不与其产生新的层叠关系
      position: static;
    }
  }
</style>
